<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CORS</title>
    <style>
        #result {
            width: 200px;
            height: 100px;
            border: solid 1px #90b;
        }
    </style>
</head>
<body>
    <button>发送请求</button>
    <div id="result"></div>
    <script>
        const btn = document.querySelector('button');
        btn.onclick = function() {
            //1. 创建对象
            const xhr = new XMLHttpRequest();
            //2. 初始化设置
            xhr.open('GET', 'http://localhost:8000/cors-server');
            //3. 发送
            xhr.send();
            //4. 绑定事件
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        //输出响应体
                        console.log(xhr.response);
                    }
                }
            }
        }
    </script>
</body>
</html>